package com.sls.web.manager.action.system;

import java.util.List;

import javax.annotation.Resource;
import com.google.gson.Gson;
import com.sls.core.common.CommonUtil;
import com.sls.web.manager.action.SuperAction;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.freemarker.CustomFreemarkerMethod;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.sys.pojo.Permission;
import com.sls.web.manager.sys.querybean.PermissionQuery;
import com.sls.web.manager.sys.service.PermissionService;

public class PermissionAction extends SuperAction {

	private static final long serialVersionUID = 1L;
	@Resource(name = "permissionService")
	private PermissionService permissionService;
	private Permission permission;
	private String code;
	private String level;
	private String flag;
	private List<Permission> permissionList;
	private PermissionQuery permissionQuery;

	public Pagination<?> page;

	Gson gson = CommonUtil.gson();

	// 添加
	public String add() {
		return INPUT;
	}

	/**
	 * 保存
	 * 
	 * @return
	 */
	public String save() {
		try {
			permission.setUrl(CustomFreemarkerMethod.dispose(permission
					.getUrl()));
			permissionService.add(permission, this.getCurrentOperator());
			redirectionUrl = "permission!list.htm";
			return SUCCESS;
		} catch (ManagerException e) {
			permission = null;
			addActionError(e.getMessage());
			return this.add();
		} catch (Exception e) {
			addActionError("系统忙！");
			return this.add();
		}

	}

	/**
	 * 查询列表
	 * 
	 * @return
	 */
	public String list() {
		if (permissionQuery == null) {
			permissionQuery = new PermissionQuery();
			permissionQuery.setPageSize(Constants.INIT_PAGESIZE);
			permissionQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
		}
		if("true".equals(flag))
			permissionQuery.setIsMenu(true);
		else if("false".equals(flag))
			permissionQuery.setIsMenu(false);
		try {
			page = permissionService.queryPermission(permissionQuery,
					getCurrentOperator());
		} catch (Exception e) {
			addActionError(e.getMessage());
		}

		return LIST;
	}

	/**
	 * 编辑
	 * 
	 * @return
	 * @throws Exception 
	 */
	public String edit() throws Exception {
		permission = permissionService.queryInfoByServiceCode(
				code, this.getCurrentOperator());
		return INPUT;
	}
	public String editPermission() throws Exception{
		try {
			permission.setUrl(CustomFreemarkerMethod.dispose(permission
					.getUrl()));
			permissionService.update(permission, getCurrentOperator());
			redirectionUrl = "permission!list.htm";
			return "success";
		} catch (ManagerException e) {
			addActionError(e.getMessage());
			code=permission.getCode();
			return this.edit();
		} catch (Exception e) {
			addActionError("系统忙！");
			code=permission.getCode();
			return this.edit();
		}
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	public String delete() {
		permissionService.delete(code, getCurrentOperator());
		redirectionUrl = "permission!list.htm";
		return SUCCESS;
	}

	public Permission getPermission() {
		return permission;
	}

	public void setPermission(Permission permission) {
		this.permission = permission;
	}

	public List<Permission> getPermissionList() {
		return permissionList;
	}

	public void setPermissionList(List<Permission> permissionList) {
		this.permissionList = permissionList;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public Pagination<?> getPage() {
		return page;
	}

	public void setPage(Pagination<?> page) {
		this.page = page;
	}

	public PermissionQuery getPermissionQuery() {
		return permissionQuery;
	}

	public void setPermissionQuery(PermissionQuery permissionQuery) {
		this.permissionQuery = permissionQuery;
	}

	public String getLevel() {
		return level;
	}

	public void setLevel(String level) {
		this.level = level;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

}
